package cn.net.zidian.mvvm.activity;

import android.view.LayoutInflater;
import android.view.View;

import androidx.lifecycle.Observer;

import com.blankj.utilcode.util.RegexUtils;
import com.blankj.utilcode.util.ToastUtils;

import cn.net.zidian.bean.HistoryBean;
import cn.net.zidian.bean.ShouJiBean;
import cn.net.zidian.databinding.ActivityShoujiSearchBinding;
import cn.net.zidian.mvvm.vm.ShoujiVM;
import cn.net.zidian.util.HistoryData;

public class ShoujiActivity extends BaseActivity<ShoujiVM> {

    private ActivityShoujiSearchBinding viewBinding;

    @Override
    protected ShoujiVM viewModel() {
        return new ShoujiVM();
    }

    @Override
    protected void initView() {
        viewBinding = ActivityShoujiSearchBinding.inflate(LayoutInflater.from(this));
        setContentView(viewBinding.getRoot());
        viewBinding.titleBar.getLeftView().setOnClickListener(v -> finish());
        initObserver();
        viewBinding.tvSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phone = viewBinding.etSearch.getText().toString();
                if (RegexUtils.isMobileExact(phone)) {
                    HistoryData.list.add(new HistoryBean(phone, "手机号码归属地"));
                    vm.search(phone);
                } else {
                    ToastUtils.showShort("请输入正确的手机号");
                }
            }
        });
    }

    private void initObserver() {
        vm.event.observe(this, new Observer<ShouJiBean>() {
            @Override
            public void onChanged(ShouJiBean shouJiBean) {
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append("运营商:").append(shouJiBean.getCompany())
                        .append("\n").append("所属地:").append(shouJiBean.getProvince()).append(shouJiBean.getCity())
                        .append("\n").append(shouJiBean.getCardtype());
                viewBinding.tvContent.setText(stringBuilder);
            }
        });

    }


}
